package ar.com.spsolutions.splilabrary.view.application.security;

import org.apache.wicket.Component;
import org.apache.wicket.authorization.Action;
import org.apache.wicket.authroles.authorization.strategies.role.AbstractRoleAuthorizationStrategy;
import org.apache.wicket.authroles.authorization.strategies.role.IRoleCheckingStrategy;
import org.apache.wicket.authroles.authorization.strategies.role.annotations.AnnotationsRoleAuthorizationStrategy;
import org.apache.wicket.authroles.authorization.strategies.role.metadata.MetaDataRoleAuthorizationStrategy;
import org.apache.wicket.request.component.IRequestableComponent;

public class UserRolesAuthorizer extends AbstractRoleAuthorizationStrategy {

	private final MetaDataRoleAuthorizationStrategy metaDataRoleAuthorizationStrategy;
	private final AnnotationsRoleAuthorizationStrategy annotationsRoleAuthorizationStrategy;

	public UserRolesAuthorizer(final IRoleCheckingStrategy roleCheckingStrategy) {
		super(roleCheckingStrategy);
		this.metaDataRoleAuthorizationStrategy = new MetaDataRoleAuthorizationStrategy(
				roleCheckingStrategy);
		this.annotationsRoleAuthorizationStrategy = new AnnotationsRoleAuthorizationStrategy(
				roleCheckingStrategy);
	}

	@Override
	public <T extends IRequestableComponent> boolean isInstantiationAuthorized(
			final Class<T> componentClass) {
		boolean annotationInstantiationAuthorized = this.annotationsRoleAuthorizationStrategy
						.isInstantiationAuthorized(componentClass);
		boolean metadataInstantiationAuthorized = this.metaDataRoleAuthorizationStrategy
				.isInstantiationAuthorized(componentClass);
		return annotationInstantiationAuthorized
				&& metadataInstantiationAuthorized;
	}

	@Override
	public boolean isActionAuthorized(final Component component,
			final Action action) {
		boolean annotationActionAuthorized = this.metaDataRoleAuthorizationStrategy.isActionAuthorized(
						component, action);
		boolean metatdataActionAuthorized = this.annotationsRoleAuthorizationStrategy
				.isActionAuthorized(component, action);
		return annotationActionAuthorized
				&& metatdataActionAuthorized;
	}

}
